package com.moorror.codegenvel.view.window.codegen.domain;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Set;

/**
 * @Author jiyanghuang
 * @Date 2025/8/27 21:44
 */
public class TableConstant {

    /**
     * 整个实现思路？
     */
    public static final String TYPES_KEY = "com.moorror.codegenvel.view.window.codegen.domain.TableConstant#TYPES_KEY_VALUES";
    public static final String KEY_PAGE_QUERY_TYPE = "com.moorror.codegenvel.view.window.codegen.domain.TableConstant#KEY_PAGE_QUERY_TYPE_VALUES";
    public static final String KEY_WIDGET_TYPE = "com.moorror.codegenvel.view.window.codegen.domain.TableConstant#KEY_WIDGET_TYPE_VALUES";

//    public static final String[] TYPES_KEY_VALUES = new String[]{
//            "java.lang.String",
//            "java.lang.Integer",
//            "java.lang.Long",
//            "java.util.Boolean",
//            "java.util.Date",
//            "java.time.LocalDateTime",
//            "java.time.LocalDate",
//            "java.time.LocalTime",
//            "java.lang.Short",
//            "java.lang.Byte",
//            "java.lang.Character",
//            "java.math.BigDecimal",
//            "java.math.BigInteger",
//            "java.lang.Double",
//            "java.lang.Float",
//            "java.lang.String[]",
//            "java.util.List",
//            "java.util.Set",
//            "java.util.Map",
//    };


    public static final Map<String, Set<String>> TYPE_MAP = Maps.newLinkedHashMap();
    public static final Map<String, String> TYPE_WIDGET_MAP = Maps.newLinkedHashMap();

    static {
        TYPE_MAP.put("java.lang.String", Sets.newHashSet("text","varchar", "VARCHAR"));
        TYPE_MAP.put("java.lang.Integer", Sets.newHashSet("int","INT"));
        TYPE_MAP.put("java.lang.Long", Sets.newHashSet("bigint", "BIGINT", "BIGINT UNSIGNED", "TIMESTAMP"));
        TYPE_MAP.put("java.util.Boolean", Sets.newHashSet(""));
        TYPE_MAP.put("java.util.Date", Sets.newHashSet(""));
        TYPE_MAP.put("java.time.LocalDateTime", Sets.newHashSet(""));
        TYPE_MAP.put("java.time.LocalDate", Sets.newHashSet(""));
        TYPE_MAP.put("java.time.LocalTime", Sets.newHashSet("timestamp"));
        TYPE_MAP.put("java.lang.Short", Sets.newHashSet("tinyint", "BIT"));
        TYPE_MAP.put("java.lang.Byte", Sets.newHashSet(""));
        TYPE_MAP.put("java.lang.Character", Sets.newHashSet(""));
        TYPE_MAP.put("java.math.BigDecimal", Sets.newHashSet(""));
        TYPE_MAP.put("java.math.BigInteger", Sets.newHashSet(""));
        TYPE_MAP.put("java.lang.Double", Sets.newHashSet(""));
        TYPE_MAP.put("java.lang.Float", Sets.newHashSet(""));
        TYPE_MAP.put("java.lang.String[]", Sets.newHashSet(""));
        TYPE_MAP.put("java.util.List", Sets.newHashSet(""));
        TYPE_MAP.put("java.util.Set", Sets.newHashSet(""));
        TYPE_MAP.put("java.util.Map", Sets.newHashSet(""));

        /**
         * XRender widget
         */
        TYPE_WIDGET_MAP.put("voidTitle", "void");
        TYPE_WIDGET_MAP.put("input", "string");
        TYPE_WIDGET_MAP.put("inputNumber", "number");
        TYPE_WIDGET_MAP.put("select", "string");
        TYPE_WIDGET_MAP.put("multiSelect", "array");
        TYPE_WIDGET_MAP.put("radio", "string");
        TYPE_WIDGET_MAP.put("checkboxes", "array");
        TYPE_WIDGET_MAP.put("textArea", "string");
        TYPE_WIDGET_MAP.put("datePicker", "string");
        TYPE_WIDGET_MAP.put("dateRange", "range");
        TYPE_WIDGET_MAP.put("timePicker", "string");
        TYPE_WIDGET_MAP.put("timeRange", "range");
        TYPE_WIDGET_MAP.put("html", "string");
        TYPE_WIDGET_MAP.put("switch", "boolean");
        TYPE_WIDGET_MAP.put("checkbox", "boolean");
        TYPE_WIDGET_MAP.put("slider", "number");
        TYPE_WIDGET_MAP.put("imageInput", "string");
        TYPE_WIDGET_MAP.put("color", "string");
        TYPE_WIDGET_MAP.put("urlInput", "string");
    }

    public static final String[] TYPES_KEY_VALUES = TYPE_MAP.keySet().toArray(new String[0]);
    public static final String[] KEY_PAGE_QUERY_TYPE_VALUES = new String[]{
            "",
            "like",
            "=",
            ">=",
            "<=",
            "in"

    };

    public static final String[] KEY_WIDGET_TYPE_VALUES = TYPE_WIDGET_MAP.keySet().toArray(new String[0]);

    /**
     * 根据sqlType 获取 javaType
     * @param sqlType
     * @return
     */
    public static String fetchType(String sqlType) {
        String sqlTypeLowerCase = StringUtils.lowerCase(sqlType);
        for (Map.Entry<String, Set<String>> entry : TYPE_MAP.entrySet()) {
            if (entry.getValue().contains(sqlTypeLowerCase)) {
                return entry.getKey();
            }
        }
        return "";
    }

    public static String valueTypeByWidget(String widget){
        return TYPE_WIDGET_MAP.get(widget);
    }
}
